<?php
/**
 * 考勤系统用户
 */
namespace app\attendance\controller;

use app\attendance\Base;
use think\Config;
use think\Db;
use think\Loader;
use think\exception\HttpException;
use baseutils\BaseUtil; # 验证类

use com\gdl\kq\interfaces\AttAdminUserServiceClient;
use com\gdl\kq\dataobject\adminuser\UserDTO; 
use com\gdl\kq\dataobject\adminuser\UserInfoDTO; 
use com\gdl\kq\dataobject\adminuser\UserPositionDTO; 
use com\gdl\kq\dataobject\adminuser\UserEditDTO; 
use com\gdl\kq\dataobject\adminuser\UserEditPhoto; 
use com\gdl\kq\dataobject\adminuser\PositionDTO; 


class AdminUser extends Base
{
	
	//用户列表
	public function getMemberList()
    {
    	$msg = ['msg'=>'暂无数据','code'=>'400','data'=>[]];

		# 公共参数
		$adminuserid = $this->request->param('adminuserid') ? $this->request->param('adminuserid') : 0;
		$reg = $this->request->param('reg') ? getStr($this->request->param('reg')) : '';
		$plaformId = $this->request->param('plaformId') ? getStr($this->request->param('plaformId')) : 1; #平台id
		$unitId = $this->request->param('unitId') ? getStr($this->request->param('unitId')) : 1; #单位id
		if(empty($plaformId)){ $msg['msg'] = "请选择平台"; return json_encode($msg);}
		// if(empty($unitId)){ $msg['msg'] = "请选择单位"; return json_encode($msg);}

		#其他参数
		$liekName = $this->request->param('name') ? getStr($this->request->param('name')) : ''; 
		$departmentId = $this->request->param('departmentId') ? getStr($this->request->param('departmentId')) : '';
		$positionId =  $this->request->param('positionId') ? getStr($this->request->param('positionId')) : '';
		$offset = $this->request->param('offset') ? getStr($this->request->param('offset')) : ''; #分页
		$num = $this->request->param('num') ? getStr($this->request->param('num')) : 10; #条数


		#其他参数
		try {
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlAgkSdk'));
			$api = new AttAdminUserServiceClient(NULL);
			\ApiClient::build($api); 
			$userDO = new UserDTO();
			$userWhere = " plaform_id = ".$plaformId." and status=0 ";
			
			# 如果有单位就查该单位的，没有就查为零的属于该平台
			if($unitId){
				$unitId=$unitId;
			}
			if($departmentId){
				$unitId=$departmentId;
			}
			if($unitId){
				$userWhere.=" and  unitid=".$unitId." ";		
			}else{
				$userWhere.=" and  unitid=0 ";		
			}	

			if($liekName){
				$userWhere.=" and realName like '%".$name."%' ";	
			}
			#职位赛选
			if($positionId){
				$userWhere.=" and positionId = ".$positionId." ";	
			}
			$userDO->where = $userWhere;

			$userDO->offset = $offset; 
			$userDO->num = $num; 
			$result = $api->getMemberList($userDO);
			if($result->success == true){
				$msg['msg'] = $result->message;
				$msg['code'] = $result->code;
				$msg['data'] = $result->data;
				if(empty($result->data)){
					$msg['msg'] = "暂无数据";
				}	 
			}

		} catch (Exception $ex) {
			$msg['msg'] = $ex->getMessage();
		}
		return json_encode($msg);	 
	}

	//添加人员
	public function addUser(){

		$msg = ['msg'=>'添加失败','code'=>'400','data'=>[]];

		# 公共参数
		$adminuserid = $this->request->param('adminuserid') ? $this->request->param('adminuserid') : 0;
		$reg = $this->request->param('reg') ? getStr($this->request->param('reg')) : '';
		$plaformId = $this->request->param('plaformId') ? getStr($this->request->param('plaformId')) : 1; #平台id
		$unitId = $this->request->param('unitId') ? getStr($this->request->param('unitId')) : 0; #单位id
		if(empty($plaformId)){ $msg['msg'] = "请选择平台"; return json_encode($msg);}
		// if(empty($unitId)){ $msg['msg'] = "请选择单位"; return json_encode($msg);}

		#其他参数 
		$name = $this->request->param('name') ? getStr($this->request->param('name')) : '唐川';
		$idcard = $this->request->param('idcard') ? getStr($this->request->param('idcard')) : '513723199409192914';
		$password =$this->request->param('password') ? getStr($this->request->param('password')) : '123456';
		$phone = $this->request->param('phone') ? getStr($this->request->param('phone')) : '13883121541';
		$workNumber = $this->request->param('workNumber') ? getStr($this->request->param('workNumber')) : '001';
		$departmentId = $this->request->param('departmentId') ? getStr($this->request->param('departmentId')) : '1';
		$positionId = $this->request->param('positionId') ? getStr($this->request->param('positionId')) : '1';
		$isLeader = $this->request->param('isLeader') ? getStr($this->request->param('isLeader')) : 0; # 0：普通员工 1：领导
		$status = $this->request->param('status') ? getStr($this->request->param('status')) : 0; # 0：正常 1：删除

		#验证
		$util = new BaseUtil();
		if (!$util->isIdCard($idcard)) {
            $msg['msg'] = "请输入正确的身份证";
            return json_encode($msg);
        }
        if (!$util->IsMobile($phone) && !empty($phone)) {
            $msg['msg'] = "请输入正确的手机号码";
            return json_encode($msg);
        }
        if(empty($name)){ $msg['msg'] = "请输入姓名"; return json_encode($msg);}
        if(empty($password) || strlen($password)<6){ $msg['msg'] = "请输入六位密码"; return json_encode($msg);} 
        if(empty($departmentId)){ $msg['msg'] = "请选择部门"; return json_encode($msg);}
        if(empty($positionId)){ $msg['msg'] = "请选择职位"; return json_encode($msg);}
		$username = \CUtf8PY::encode($name,'all'); # 拼音账号

		#数据集
		$sdkData = [
			'userName'=>$username.mt_rand(10000,99999),
			'realName'=>$name,
			'idcard'=>$idcard,
			'phone'=>$phone,
			'jobnumber'=>$workNumber,
			'password'=>$password,
			'photoStatus'=>0,  #0: 未上传 1:未审核 2:未通过 3:已审核'
			'departmentId'=>$departmentId,
			'positionId'=>$positionId,
			'status'=>$status,
			'createTime'=>time(),
			'isCharge'=>$isLeader,
			'status'=>$status,
			'isBind'=>0,
			'plaform_id'=>$plaformId,
			'unitid'=>$unitId?$unitId:$departmentId, #如果他没有单位id 那就拿部门id做unitid	
		];
		if($phone){$sdkData['isBind'] = 1;} #是否绑定手机 

		try {
			# 实例化
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlAgkSdk'));
			$api = new AttAdminUserServiceClient(NULL);
			\ApiClient::build($api); 
			$userDO = new UserEditDTO();

			# 数据集
			$userDO->data = $sdkData; 
			$result = $api->addUser($userDO);
			if($result->success ==true){
				$msg['code'] = $result->code;
				$msg['msg'] = $result->message;
				$msg['data'] = [];
			}
		} catch (Exception $ex) {
			$msg['msg'] = $ex->getMessage();
		}
		return json_encode($msg);	
	}

	//用户详情 包含留底 取相关相关字段
	public function userInfo(){
		$msg = ['msg'=>'未找到相关用户','code'=>'400','data'=>[]];

		# 公共参数
		$adminuserid = $this->request->param('adminuserid') ? $this->request->param('adminuserid') : 0;
		$reg = $this->request->param('reg') ? getStr($this->request->param('reg')) : '';
		$plaformId = $this->request->param('plaformId') ? getStr($this->request->param('plaformId')) : 1; #平台id
		$unitId = $this->request->param('unitId') ? getStr($this->request->param('unitId')) : 0; #单位id
		if(empty($plaformId)){ $msg['msg'] = "请选择平台"; return json_encode($msg);}
		// if(empty($unitId)){ $msg['msg'] = "请选择单位"; return json_encode($msg);}
		
		#其他参数
		$userId = $this->request->param('userId') ? getStr($this->request->param('userId')) : '';

		# 验证
		if(empty($userId)){$msg['msg'] = "请选择用户"; return json_encode($msg);}

		try{
			# 实例化
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlAgkSdk'));
			$api = new AttAdminUserServiceClient(NULL);
			\ApiClient::build($api); 
			$userDO = new UserEditDTO();

			# 数据集
			$userDO->userid = $userId; 
			$result = $api->userInfo($userDO);
			if($result->success ==true){
				$msg['code'] = $result->code;
				$msg['msg'] = $result->message;
				if(!empty($result->data)){
					$msg['data'] = $result->data[0];
				}
			}
		}catch(Exception $e){
			$msg['msg'] = $e->getMessage();
		}

		return json_encode($msg);
	}

	//审核照片
	public function verifyMemberPhoto(){
		$msg = ['msg'=>'暂未审核','code'=>'400','data'=>[]];

		# 公共参数
		$adminuserid = $this->request->param('adminuserid') ? $this->request->param('adminuserid') : 0;
		$reg = $this->request->param('reg') ? getStr($this->request->param('reg')) : '';
		$plaformId = $this->request->param('plaformId') ? getStr($this->request->param('plaformId')) : 1; #平台id
		$unitId = $this->request->param('unitId') ? getStr($this->request->param('unitId')) : 0; #单位id
		if(empty($plaformId)){ $msg['msg'] = "请选择平台"; return json_encode($msg);}
		// if(empty($unitId)){ $msg['msg'] = "请选择单位"; return json_encode($msg);}
		
		#其他参数
		$userId = $this->request->param('userId') ? getStr($this->request->param('userId')) : '';
		$status = $this->request->param('status') ? getStr($this->request->param('status')) : '';
		$text = $this->request->param('photoInfo') ? getStr($this->request->param('photoInfo')) : '';

		#验证
		if(empty($userId)){$msg['msg'] = "请选择需要审核的用户"; return json_encode($msg);}

		try{
			# 实例化
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlAgkSdk'));
			$api = new AttAdminUserServiceClient(NULL);
			\ApiClient::build($api); 
			$userDO = new UserEditPhoto();
			$userDO->userid = $userId;
			$userDO->status = $userId;
			$userDO->text = $text;
			$result = $api->verifyMemberPhoto($userDO);

			if($result->success ==true){
				$msg['code'] = $result->code;
				$msg['msg'] = $result->message; 
				$msg['data'] = $result->data; 
			}
		}catch(Exception $e){
			$msg['msg'] = $e->getMessage();
		}

		return json_encode($msg);
	}

	//删除用户 单个删除 批量删除
	public function deleteOneMember(){
		$msg = ['msg'=>'删除失败','code'=>'400','data'=>[]];

		# 公共参数
		$adminuserid = $this->request->param('adminuserid') ? $this->request->param('adminuserid') : 0;
		$reg = $this->request->param('reg') ? getStr($this->request->param('reg')) : '';
		$plaformId = $this->request->param('plaformId') ? getStr($this->request->param('plaformId')) : 1; #平台id
		$unitId = $this->request->param('unitId') ? getStr($this->request->param('unitId')) : 0; #单位id
		if(empty($plaformId)){ $msg['msg'] = "请选择平台"; return json_encode($msg);}
		// if(empty($unitId)){ $msg['msg'] = "请选择单位"; return json_encode($msg);}
		
		#其他参数
		$userId = $this->request->param('userId') ? getStr($this->request->param('userId')) : '1'; #单位id

		#处理去除，，，
		$myUserId = explode(",", $userId);
		$userArray = [];
		foreach($myUserId as $v){
			if(!empty($v)){
				$userArray[] = $v;
			}
		}
		$userId = implode(",", $userArray);

		#验证
		if(empty($userId)){$msg['msg'] = "请选择用户"; json_encode($msg);}

		try{
			# 实例化
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlAgkSdk'));
			$api = new AttAdminUserServiceClient(NULL);
			\ApiClient::build($api); 
			$userDO = new UserInfoDTO();
			$userDO->idstr = $userId;
			$result = $api->deleteOneMember($userDO);

			if($result->success ==true){
				$msg['code'] = $result->code;
				$msg['msg'] = $result->message; 
				$msg['data'] = $result->data; 
			}
		}catch(Exception $e){
			$msg['msg'] = $e->getMessage();
		}

		return json_encode($msg);
	}
	 
}
